/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-11-14 下午1:30 Created by dwzhang
 */
package com.jade.modules.protocol.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.protocol.ProtocolChannel;
import com.jade.modules.protocol.ProtocolDao;
import com.jade.modules.protocol.ProtocolException;
import com.jade.modules.protocol.ProtocolParameter;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

/**
 * 采用JPA方式实现的数据访问接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-11-14 下午1:30
 */
//@Named ("module_protocolDao")
public class JpaProtocolDao implements ProtocolDao
{
    private static final String SQL_CHANNEL = "SELECT channel FROM com.jade.modules.protocol.ProtocolChannel channel";
    private static final String SQL_PARAM = "SELECT parameter FROM com.jade.modules.protocol.ProtocolParameter parameter" +
            " WHERE type=:type AND protocol=:protocol ORDER BY sortCode DESC";
    @PersistenceContext (unitName = "default")
    protected EntityManager em;

    public void addChannel(ProtocolChannel channel)
            throws ProtocolException
    {
        em.persist(channel);
    }

    public void updateChannel(ProtocolChannel channel)
            throws ProtocolException
    {
        em.merge(channel);
    }

    @Caching (evict = {@CacheEvict (value = "module.protocol.channel.g", key = "#channel.id"),
            @CacheEvict (value = "module.protocol.channel.l", key = "#channel.type + '[%GRP%]'"),
            @CacheEvict (value = "module.protocol.channel.q", key = "#channel.type + '[%GRP%]'")})
    public void removeChannel(ProtocolChannel channel)
            throws ProtocolException
    {
        ProtocolChannel old = getChannel(channel.getId());
        if (old != null) {
            em.remove(old);
        }
    }

    @Cacheable (value = "module.protocol.channel.g", key = "#id")
    public ProtocolChannel getChannel(int id)
    {
        return em.find(ProtocolChannel.class, id);
    }

    @Cacheable (value = "module.protocol.channel.q", key = "#type+'[%GRP%]'+#criteria")
    public PagingList<ProtocolChannel> queryChannel(String type, Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL_CHANNEL, em, criteria);
    }

    @Cacheable (value = "module.protocol.channel.l", key = "#type+'[%GRP%]'+#criteria")
    public List<ProtocolChannel> listChannel(String type, Criteria criteria)
    {
        criteria.add(Restrictions.eq("type",type));
        return SqlUtils.sqlQueryWithoutCount(SQL_CHANNEL, em, criteria);
    }

    @Cacheable (value = "module.protocol.param.l", key = "#type + #protocol")
    public List<ProtocolParameter> getParameters(String type, String protocol)
    {
        Query query = em.createQuery(SQL_PARAM);
        query.setParameter("type", type);
        query.setParameter("protocol", protocol);
        return query.getResultList();
    }
}
